package edu.cicese.alz;

import edu.cicese.alz.reports.AccelerometerPane;
import edu.cicese.alz.reports.CombinedPane;
import edu.cicese.alz.reports.GPSPane;
import edu.cicese.alz.reports.NFCPane;
import edu.cicese.alz.swing.CustomToggleButton;
import edu.cicese.alz.swing.SwingUtil;
import edu.cicese.alz.swing.TypeButtonListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton.ToggleButtonModel;
import javax.swing.SpinnerDateModel;
import javax.swing.text.PlainDocument;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 18/06/12
 * Time: 06:02 PM
 */
public class FrameReport extends SwingComponent {

	private CustomToggleButton btnAccelerometer, btnGPS, btnNFC, btnWIFI, btnCombined;
	private JPanel optionsPane;
	private AccelerometerPane accPane;
	private GPSPane gpsPane;
	private NFCPane nfcPane;
	private CombinedPane combinedPane;

	private FrameNFCTags frameNFCTags;

	public static ToggleButtonModel rbtUsersAllModel = new ToggleButtonModel();
	public static ToggleButtonModel rbtUsersModel = new ToggleButtonModel();
	public static ToggleButtonModel rbtPeriodAllModel = new ToggleButtonModel();
	public static ToggleButtonModel rbtPeriodFromModel = new ToggleButtonModel();
	public static ToggleButtonModel chbSaveChart = new ToggleButtonModel();
	public static ToggleButtonModel chbShowChart = new ToggleButtonModel();
	public static PlainDocument txtUsersModel = new PlainDocument();
	public static PlainDocument txtDayFromModel = new PlainDocument();
	public static PlainDocument txtDayToModel = new PlainDocument();
	public static SpinnerDateModel sHourFromModel = new SpinnerDateModel();
	public static SpinnerDateModel sHourToModel = new SpinnerDateModel();

	public static void main(String[] a) {
		SwingUtil.setUIManager();
		new FrameReport();
	}

	public FrameReport() {
		super(1000, 800, "Generate report");

		initComponents();
	}

	private void initComponents() {
		JPanel pContent = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		optionsPane = createOptionsPane();

		// Row 1
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc.insets = new Insets(5, 5, 0, 5);
		pContent.add(createBoxType(optionsPane), gbc);

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 0.5;
		gbc.weighty = 1;
		gbc.insets = new Insets(5, 0, 0, 5);
		pContent.add(optionsPane, gbc);

		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.insets = new Insets(7, 0, 7, 0);
		pContent.add(createBoxButtons(), gbc);

		pContent.setBackground(SwingUtil.cBackground);
		setContentPane(pContent);
	}


	private Box createBoxButtons() {
		JButton btnGenerate = createButtonBackground("Generate", 71, 22, null, "bg");
		JButton btnCancel = createButtonBackground("Cancel", 71, 22, null, "bg");
		JButton btnSave = createButtonBackground("Save", 71, 22, null, "bg");
		JButton btnDefaults = createButtonBackground("Defaults", 71, 22, null, "bg");

		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionGenerate();
			}
		});
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionSave();
			}
		});
		btnDefaults.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionDefaults();
			}
		});


		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(Box.createHorizontalGlue());
		boxButtons.add(btnDefaults);
		boxButtons.add(Box.createHorizontalStrut(7));
		boxButtons.add(btnSave);
		boxButtons.add(Box.createHorizontalStrut(7));
		boxButtons.add(btnGenerate);
		boxButtons.add(Box.createHorizontalStrut(7));
		boxButtons.add(btnCancel);
		boxButtons.add(Box.createHorizontalStrut(5));

		return boxButtons;
	}

	private Box createBoxType(JPanel optionsPane) {
		btnAccelerometer = SwingUtil.createCustomToggleButton("Accelerometer", null, null, true, Color.WHITE, 100, 24);
		btnGPS = SwingUtil.createCustomToggleButton("GPS", null, null, false, Color.WHITE, 100, 24);
		btnNFC = SwingUtil.createCustomToggleButton("NFC", null, null, false, Color.WHITE, 100, 24);
		btnWIFI = SwingUtil.createCustomToggleButton("WIFI", null, null, false, Color.WHITE, 100, 24);
		btnCombined = SwingUtil.createCustomToggleButton("Combined", null, null, false, Color.WHITE, 100, 24);

		CustomToggleButton[] buttons = new CustomToggleButton[]{btnAccelerometer, btnGPS, btnNFC, btnWIFI, btnCombined};

		btnAccelerometer.addMouseListener(new TypeButtonListener(buttons, 0, optionsPane, "accelerometer"));
		btnGPS.addMouseListener(new TypeButtonListener(buttons, 1, optionsPane, "gps"));
		btnNFC.addMouseListener(new TypeButtonListener(buttons, 2, optionsPane, "nfc"));
		btnWIFI.addMouseListener(new TypeButtonListener(buttons, 3, optionsPane, "accelerometer"));
		btnCombined.addMouseListener(new TypeButtonListener(buttons, 4, optionsPane, "combined"));

		Box boxType = Box.createVerticalBox();
		boxType.add(btnAccelerometer);
		boxType.add(btnGPS);
		boxType.add(btnNFC);
		boxType.add(btnWIFI);
		boxType.add(btnCombined);

		boxType.setMinimumSize(new Dimension(100, 100));
		boxType.setOpaque(true);
		boxType.setBackground(Color.WHITE);
		boxType.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1),
				BorderFactory.createLineBorder(Color.WHITE, 4)));

		return boxType;
	}

	private JPanel createOptionsPane() {
		JPanel pOptions = new JPanel(new CardLayout());

		pOptions.add("accelerometer", accPane = new AccelerometerPane());
		pOptions.add("gps", gpsPane = new GPSPane());
		pOptions.add("nfc", nfcPane = new NFCPane(frameNFCTags));
		pOptions.add("combined", combinedPane = new CombinedPane(accPane, gpsPane, nfcPane));

		return pOptions;
	}


	private void actionSave() {
		if (btnAccelerometer.isSelected()) {
			accPane.saveOptions();
		} else if (btnGPS.isSelected()) {
			gpsPane.saveOptions();
		} else if (btnNFC.isSelected()) {
			nfcPane.saveOptions();
		} else if (btnCombined.isSelected()) {
			combinedPane.saveOptions();
		}
	}

	private void actionGenerate() {
		if (btnAccelerometer.isSelected()) {
			accPane.generate();
		} else if (btnGPS.isSelected()) {
			gpsPane.generate();
		} else if (btnNFC.isSelected()) {
			nfcPane.generate();
		} else if (btnCombined.isSelected()) {
			combinedPane.generate();
		}
	}

	private void actionCancel() {}

	private void actionDefaults() {
		if (btnAccelerometer.isSelected()) {
			accPane.loadDefaultOptions();
		} else if (btnGPS.isSelected()) {
			gpsPane.loadDefaultOptions();
		} else if (btnNFC.isSelected()) {
			nfcPane.loadDefaultOptions();
		} else if (btnCombined.isSelected()) {
			combinedPane.loadDefaultOptions();
		}
	}
}
